// pages/my/product-order/product-order.js
const controllers = require('../../controllers/ControllerMy').controller;
const controller=require("../../controllers/ControllerVip").controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    orderList: [],
    activenav: 0,
    imgurl: '',
    // navlist: ['全部', '待取货', '取货完成', '待拼团', '待评价'],
    orderTypeList:['线上订单','线下订单','云商品订单','已退订单'],
    navall: false,
    navchoice: '',
    isshow:false,
    singleord:'',
    buttons: [{text: '取消'}, {text: '确定'}],
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
    selectNo:"",
    selectIndex:0,
    type:1,
    lastPage:false,
    states:'',
    currentIndex:0
  },

  changeOrderType(e){
    let index=null
    console.log(e);
    if(e){
       index=e.currentTarget.dataset.index
    }else{
       index=this.data.currentIndex
    }
    this.setData({
      currentIndex:index,
      orderList:[],
      pageNumber:1
    })
    if(index==0){
      this.setData({
        pageNumber:1,
        type:1,
        selectIndex:index,
        states:''
      })
      this.orderList()
    }else if(index==1){
      this.setData({
        pageNumber:1,
        type:1,
        selectIndex:index
      })
      this.getLowOrderList()
    }else if(index==2){
      this.setData({
        pageNumber:1,
        type:2,
        selectIndex:index,
        states:''
      })
      this.orderList()
    }else{
      this.setData({
        pageNumber:1,
        type:'',
        selectIndex:index,
        states:'6'
      })
      this.orderList()
    }
 },

  getMore(){
    if(this.data.lastPage==false){
        wx.showLoading({
          title: '加载中',
        })
        let value={}
        if(this.data.selectIndex==0||this.data.selectIndex==2){
          value.pageNumber=this.data.pageNumber+1
          this.setData({
            pageNumber:value.pageNumber
          })
          value.pageSize=this.data.pageSize
          value.shopId=app.globalData.shopId
          value.type=this.data.type

          controllers.getOrderList(value).then(res => {
            let list=this.data.orderList
            let list2=res.data.list
            let list3=list.concat(list2)
            wx.hideLoading({
              success: (res) => {},
            })
            this.setData({
              orderList: list3,
              lastPage:res.data.lastPage
            })
          })
        }else{
          value.pageNumber=this.data.pageNumber+1
          this.setData({
            pageNumber:value.pageNumber
          })
          value.shopId=wx.getStorageSync('shopId')
          value.genre=1
          value.amountType=0
          value.lowLine=1
          value.types="1,6,7"
          controller.amountHistory(value).then(res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            let list=this.data.orderList
            let list2=res.data.list
            let list3=list.concat(list2)
            this.setData({
              orderList: list3,
              lastPage:res.data.lastPage
            })
          })
        }
    }
  },

  //获取线下商品订单列表
  getLowOrderList(){
    wx.showLoading({
      title: '加载中',
    })
    let value={}
    value.pageNumber=this.data.pageNumber
    value.shopId=wx.getStorageSync('shopId')
    value.genre=1
    value.amountType=0
    value.lowLine=1
    value.types="1,6,7"
    controller.amountHistory(value).then(res=>{
      wx.hideLoading({
        success: (res) => {},
      })
      this.setData({
        orderList: res.data.list,
        lastPage:res.data.lastPage
      })
    })
 },

  //获取列表
  orderList(state){
    wx.showLoading({
      title: '加载中',
    })
    controllers.getOrderList({
      pageNumber: this.data.pageNumber,
      pageSize: this.data.pageSize,
      shopId: app.globalData.shopId,
      type: this.data.type,
      states:this.data.states
    }).then(res => {
        wx.hideLoading({
          success: (res) => {},
        })
        this.setData({
          orderList: res.data.list,
          lastPage:res.data.lastPage
        })
    })
  },

  //按钮事件
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.index == 0) {
      this.setData({
        isshow: false
      })
    } else {
      this.cancelOrder()
      this.setData({
        isshow:false
      })
    }
  },

  //申请核销
  applyWriteOff(e){
    let that=this
    this.setData({
      selectNo:e.currentTarget.dataset.no,
      selectGoodsIndex:e.currentTarget.dataset.index
    })
    wx.showModal({
      title: '提示',
      content: '是否完成订单或申请核销该商品',
      success (res) {
        if (res.confirm) {
          that.writeOff(that.data.selectGoodsIndex)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  writeOff(index){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.orderNo=this.data.selectNo
    controllers.applyWriteOff(value).then(res=>{
      wx.showToast({
        icon:"none",
        title: '操作成功',
      })
      this.data.orderList[index].state=12
      this.setData({
        orderList:this.data.orderList
      })
    })
  },

  //显示取消确定弹窗
  toshow(e){
    this.setData({
      isshow:true,
      singleord:e.currentTarget.id
    })
  },
  //取消订单
cancelOrder(e){
  controllers.cancelOrder({
    shopId:app.globalData.shopId,
    orderNo:this.data.singleord,
    type:1
  }).then(res =>{
    wx.showToast({
      title: '取消成功',
      icon:'none'
    })
    this.setData({
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      orderList:[]
    })
    this.orderList(this.data.navchoice)
  })
},

//跳转到订单详情页
toDetail(e){
  if(this.data.selectIndex==1){
    wx.navigateTo({
      url: '/pages/my/lowLineOrder-detail/lowLineOrder-detail?orderNo='+e.currentTarget.id,
    })
  }else if(this.data.selectIndex==0){
    wx.navigateTo({
      url: '/pages/my/productOrder-detail/productOrder-detail?id='+e.currentTarget.id,
    })
  }else{
    wx.navigateTo({
      url: '/pages/my/cloudproducOrder-detail/cloudproductOrder-detail?id='+e.currentTarget.id,
    })
  }
},

//跳转到评论页面
toComment(e){
  wx.navigateTo({
    url: '/pages/my/service-comment/service-comment?id='+e.currentTarget.id+'&type='+e.currentTarget.dataset.type+'&from='+e.currentTarget.dataset.from,
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      imgurl:app.globalData.IMG_URL
    })
    this.setData({
      navchoice: options.id
    })

    if (options.id == 2) {
      this.setData({
        activenav: 1
      })
    } else if (options.id == 9) {
      this.setData({
        activenav: 2
      })
    } else if (options.id == 8) {
      this.setData({
        activenav: 4
      })
    } else if (options.id == 1) {
      this.setData({
        activenav: 3
      })
    }


    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      orderList: [],
      isEnd: false,
      pageNumber: 1,
    })
    this.changeOrderType()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})